package com.pbc.gmp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pbc.gmp.dao.AclMapper;
import com.pbc.gmp.dao.UserRoleMapper;
import com.pbc.gmp.model.RoleC;
import com.pbc.gmp.model.basemodel.Acl;
import com.pbc.gmp.model.basemodel.Role;
import com.pbc.gmp.dao.RoleMapper;
import com.pbc.gmp.model.basemodel.UserRole;
import com.pbc.gmp.service.IRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource(name="roleDao")
    private RoleMapper roleMapper;
    @Resource(name = "permissionDao")
    private AclMapper aclMapper;
    @Resource(name = "userRoleDao") private UserRoleMapper userRoleMapper;
    @Override
    public Set<String> selectRolesListByUserId(Long id) {
        return roleMapper.selectRolesListByUserId(id);
    }

    @Override
    public Set<Role> selectRoleListByUserId(Long id) {
        return roleMapper.selectRoleListByUserId(id);
    }

    @Override
    public List<RoleC> selectRoleClist() {
        return  roleMapper.selectRoleClist();
    }

    @Override
    public Set<Long> selectRoleIdsByRoleName(Set<String> names) {
        Set<Long> roleids =new HashSet<Long>();
        for (String name : names) {
            Role r=new Role();
            r.setName(name);
            r=roleMapper.selectOne(r);
            roleids.add(r.getId());
        }
        return roleids;

    }

    @Override
    public void deleteBatchByIds(String ids) {
        String[] idlist =ids.split(",");

        for (String id : idlist) {
            roleMapper.deleteById(Convert.toLong(id));
            aclMapper.delete(new EntityWrapper<Acl>().eq("pType","role").eq("pId",Convert.toLong(id)));
            userRoleMapper.delete(new EntityWrapper<UserRole>().eq("roleId",Convert.toLong(id)));
            System.out.print(id);
        }
    }
}
